package com.fr;

import org.apache.commons.lang3.StringUtils;

/**
*作者：furong
*日期：2016年12月16日
*时间：下午8:00:22
*/
public class hhhh {
    public static final char[] charMap = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
            'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
            'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '_' };

    public static int toNumber(char c) {
        for (int i = 0, len = charMap.length; i < len; i++) {
            if (charMap[i] == c) {
                return i;
            }
        }
        return -1;
    }

    public static String numberTo62(long src) {
        int to = 63;
        long left = to;
        StringBuilder b = new StringBuilder();

        while (left >= to) {
            left = src / to;

            b.append(charMap[(int) (src % to)]);

            src = left;
        }
        if (left > 0) {
            b.append(charMap[(int) left]);
        }
        return StringUtils.reverse(b.toString());
    }

    public static Long numberFrom62(String v) {

        char[] vArr = v.toCharArray();
        long rs = 0;
        for (char c : vArr) {
            int i = toNumber(c);
            if (i < 0) {
                return null;
            }
            rs = rs * 63 + i;
        }

        return rs;
    }

    public static void main(String[] args) {
        int i = 0;
        String v = "";
        long begin = System.currentTimeMillis();
        // while (i < 10000000) {
        // i++;
        // v = numberTo62(i);
        // }
        i = 10000000;
        v = numberTo62(i);
        System.out.println((System.currentTimeMillis() - begin) + " ms");
        System.out.println(i + " : " + v);
        System.out.println(numberFrom62(v));

        System.out.println(numberTo62(Long.MAX_VALUE));
    }

}
